package com.eportal.controller.background;

import com.alibaba.fastjson.JSONObject;
import com.eportal.action.background.CInstallUnitAction;
import com.eportal.domain.Sysopt;
import com.eportal.service.CInstallUnitService;
import com.eportal.vo.controller.background.cinstallunit.CInstallUnitSelectListVO;
import com.eportal.vo.controller.background.cinstallunit.ListBudingAndUnitVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 单元管理
 * Created by Saber on 2017/8/10.
 */
@Controller
@RequestMapping("/admin/cinstallunit")
public class CinstallUnitController {

    private static Logger logger = LoggerFactory.getLogger(CinstallUnitController.class.getName());

    @Resource
    private CInstallUnitService cInstallUnitService;


    /**
     * 单元列表（下拉）
     * @author Saber
     * @Date 2017/8/10 上午9:38
     * @param request
     * @param cbuildingId   楼栋id
     * @see CInstallUnitAction#listUnit()
     * @return
     */
    @RequestMapping(value = "selectList.json")
    @ResponseBody
    public JSONObject selectList(HttpServletRequest request,String cbuildingId){

        JSONObject json = new JSONObject();

        Sysopt user= (Sysopt)request.getSession().getAttribute("user");
        int userRole = (Integer)request.getSession().getAttribute("userRole");

        try {
            List<CInstallUnitSelectListVO> list=cInstallUnitService.selectList(cbuildingId,user,userRole);
            json.put("code",1);
            json.put("msg","ok");
            json.put("data",list);
            return json;
        }catch (Exception e){
            e.printStackTrace();
            logger.error("获取单元列表（下拉）异常",e);
            json.put("code",0);
            json.put("msg","获取列表异常:"+e.getMessage());
            json.put("data",null);
            return json;
        }
    }

    /**
     * 所有的楼栋单元列表
     * @author Saber
     * @Date 2017/8/22 下午5:05
     * @param request
     * @param communityId   小区id
     * @see CInstallUnitAction#listBudingAndUnit()
     * @return
     */
    @RequestMapping(value = "listBudingAndUnit.json")
    @ResponseBody
    public JSONObject listBudingAndUnit(HttpServletRequest request,String communityId){

        JSONObject json = new JSONObject();

        Sysopt user= (Sysopt)request.getSession().getAttribute("user");
        int userRole = (Integer)request.getSession().getAttribute("userRole");

        try {
            List<ListBudingAndUnitVO> list=cInstallUnitService.listBudingAndUnit(communityId, user, userRole);
            json.put("code",1);
            json.put("msg","ok");
            json.put("data",list);
            return json;
        }catch (Exception e){
            e.printStackTrace();
            logger.error("获取单元列表（下拉）异常",e);
            json.put("code",0);
            json.put("msg","获取列表异常:"+e.getMessage());
            json.put("data",null);
            return json;
        }
    }
}
